package com.xjj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjj.entity.ResponseEntity;
import com.xjj.entity.School;
import com.xjj.entity.Student;
import com.xjj.excel.ExcelUtils;
import com.xjj.service.SchoolService;
import com.xjj.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author sxy
 * @version 1.0
 * @date 2022/10/19 20:48
 */
@RestController
@Api(value = "该类是用户模块的api接口")
@RequestMapping("/manage/admin/manageTest")
public class TestController {
    @Resource
    private TestService testService;

    @Resource
    private SchoolService schoolService;

    @GetMapping("/test1")
    public String test1(){
        return "ok";
    }

    @GetMapping("/test2")
    @ApiOperation(value = "查询所有用户" ,notes = "得到所有用户信息")
    @ApiResponse(code = 200,message = "服务器内部异常",response = Exception.class)
    public ResponseEntity test2(){
        List<Student> allSchool = testService.findAllSchool();
        return new ResponseEntity(200,"ok",allSchool);
    }

    @GetMapping("/test3")
    @ApiOperation(value = "mp测试" ,notes = "通过id")
    @ApiResponse(code = 200,message = "服务器内部异常",response = Exception.class)
    public ResponseEntity test3(){
        School sc = testService.getById(1);
        return new ResponseEntity(200,"ok",sc);
    }

    @GetMapping("/test")
    @ApiOperation(value = "分页测试")
    public ResponseEntity test(){
        Page<School> producePage = new Page<>(2,3);
        Page<School> page = schoolService.page(producePage);
        System.out.println(producePage == page);
        List<School> schools = page.getRecords();
        for (School school : schools) {
            System.out.println(school);
        }
        return new ResponseEntity(200,"ok",schools);
    }


    @PostMapping("/import")
    @ApiOperation(value = "excel导入测试")
    public void importSchool(@RequestPart("file")MultipartFile file) throws Exception {
        List<School> schools = ExcelUtils.readMultipartFile(file, School.class);
        for (School school : schools) {
            System.out.println(school.toString());
        }
    }

    private static final String RootPath="E:\\";
    private static final String FileFormat=".png";
    private static final ThreadLocal<SimpleDateFormat> LOCALDATEFORMAT=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));

    @PostMapping("generate/v1")
    @ApiOperation(value = "保存到本地")
    public ResponseEntity generateV1(String content){
        String fileName = LOCALDATEFORMAT.get().format(new Date());
        return ResponseEntity.SUCCESS("ok",200);
    }
}
